package top.fate.until;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import top.fate.entity.NewWord;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Slf4j
public class NewFileReading {
    public List<NewWord> getFile() throws Exception {
        List<NewWord> li = new ArrayList<>();
        try {
            //读取全部内容
            InputStream is = new FileInputStream("D:\\c\\测试.docx");
            XWPFDocument doc = new XWPFDocument(is);
            List<XWPFParagraph> paragraphs = doc.getParagraphs();
            XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(doc);
            //获得全部文档
            String text1 = xwpfWordExtractor.getText();

            //System.out.println(text1);
            //获取文档转换为String集合
            //   for (XWPFParagraph paragraph : paragraphs) {
            //       String text = paragraph.getText();
            //       li.add(text);
            //   }
            // String join = String.join(",", li);
            // System.out.println(join);

            //获取word标题
            List<String> list = new ArrayList<>();
            for (XWPFParagraph paragraph : paragraphs) {
                String text = paragraph.getParagraphText();//
                String style = paragraph.getStyle();
                if ("1".equals(style) || "2".equals(style) || "3".equals(style) || "4".equals(style) || "5".equals(style) || "6".equals(style) || "7".equals(style) || "8".equals(style)) {
                    //将标题添加到list集合中
                    list.add(text);
                }
            }
           // log.error(s3);
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).isEmpty()) {
                    //删除标题为空的
                    list.remove(i);
                    // log.error(list.get(i));
                }
            }
            for (int i = 0; i < list.size(); i++) {
                NewWord word = new NewWord();
                word.setName("测试.docx");
                word.setId(UUID.randomUUID().toString());
                //获取第一个标题
                String s = list.get(i);
               // log.info(s);
                //判断不是最后一个标题
                if (i == 0){
                    String s3 = StringUtils.substringBefore(text1, list.get(0));
                    word.setTitle("前篇");
                    word.setContent(s3);

                }
                if (i + 1 < list.size()) {
                    String s1 = list.get(i + 1);
                    //log.error(s1);
                    //根据第一个标题和下一个标题截取中间内容
                    String s2 = StringUtils.substringBetween(text1, s, s1);
                    word.setTitle(s);
                    word.setContent(s2);
                    li.add(word);
                   // log.warn(s2);
                }
                //判断如果是最后一个标题
                if (i == list.size() - 1) {
                    //根据最后一个标题截取到最后内容
                    // String s2 = StringUtils.substringBetween(text1, s);
                    String s2 = StringUtils.substringAfter(text1, s);
                    word.setTitle(s);
                    word.setContent(s2);
                    li.add(word);
                 //   log.error(s2);
                }
            }
            //         //获取图片
            //         List<XWPFPictureData> allPictures = doc.getAllPictures();
            //         for (XWPFPictureData allPicture : allPictures) {
            //             byte[] data = allPicture.getData();
            //             System.out.println(data);
            //             FileOutputStream fos = new FileOutputStream("D:\\B\\images.txt");
            //             fos.write(data);
            //             System.out.println("==========================");
            //         }
            //        // System.out.println(allPictures);//
            //         this.close(is);//
        } catch (Exception e) {
            e.printStackTrace();
        }
        return li;
    }
}
